#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""GIT login handler

For GIT login page handler.
"""

__author__ = 'Xin Zhou(zhounew@google.com)'

from git.logic import git_account_status_action
from git.logic import git_account_status_logic
from git.logic import git_account_status_response
from git.logic import git_login_request
from git.logic import git_password_action
from git.logic import git_password_logic
from git.logic import git_password_response

class GitLoginHandler(object):
  def __init__(self, login_action_string, email, password):
    self.login_action_string_ = login_action_string
    self.email_ = email
    self.password_ = password
    
  def Execute(self):
    if self.login_action_string_ == 'login':
      return self.HandleLogin()
    else:
      return self.HandleAction()
    
  def HandleLogin(self):
    action = git_password_action.GitPasswordAction()
    req = git_login_request.GitLoginRequest(self.email_, self.password_)
    res = git_password_response.GitPasswordResponse()
    logic = git_password_logic.GitPasswordLogic(action)
    logic.Run(req, res)
    error = res.GetError()
    if error:
      return error
    else:
      return res.GetOutput()
    
  def HandleAction(self):
    if self.login_action_string_.lower() == 'userstatus':
      action = git_account_status_action.GitAccountStatusAction()
      req = git_login_request.GitLoginRequest(self.email_, None)
      res = git_account_status_response.GitAccountStatusResponse()
      logic = git_account_status_logic.GitAccountStatusLogic(action)
      logic.Run(req, res)
      error = res.GetError()
      if error:
        return error
      else:
        return res.GetOutput()